#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2021/6/16 16:56
# @Author   : chengfeng
# @Email    : chengfeng@inhand.com.cn
# @File     : conftest.py

from playwright.sync_api import sync_playwright
import pytest
from Conf.Config import Config
import logging
from Testcase import commom_step
conf = Config()

"""
运行用例前后置条件，对浏览器的操作
"""
@pytest.fixture(scope='class')
def page():
    with sync_playwright() as playwright:
        browser = playwright.firefox.launch(headless=False)
        if conf.browser == 'chrome':
            browser = playwright.chromium.launch(headless=False)
        context = browser.new_context(ignore_https_errors=True)
        context.tracing.start(screenshots=True, snapshots=True)
        logging.info('Start your journey browser is {} address {}://{} user is {}'.format(conf.browser, conf.protocol,
                                                                                          conf.host, conf.username))
        #Open new page
        page = context.new_page()
        page.on('console', lambda msg: logging.error(msg.text) if msg.type == 'error' else None)
        logging.info("New browser page is open")
        yield page
        context.tracing.stop(path='traze.zip')
        page.close()
        context.close()
        browser.close()

@pytest.fixture(scope='class')
def login(page):
    """
    登录设备
    :param page:
    :return:
    """
    commom_step.goto_route(page)
    commom_step.input_username(page, conf.username)
    commom_step.input_password(page, conf.password)
    commom_step.submit_login(page)
    page.wait_for_url("{}://{}/overview".format(conf.protocol, conf.host))
    logging.info("{} login in web console".format(conf.username))
    yield page